package com.metaera.app.ui.fragments;

import android.graphics.Color;
import android.os.Bundle;

import com.metaera.app.R;
import com.metaera.app.controller.core.IPaginationPresenter;

import com.metaera.app.controller.presenter.HomeNewsListPresenter;
import com.metaera.app.controller.presenter.HomeTopNewsPresenter;
import com.metaera.app.controller.view.BasicPaginationView;
import com.metaera.app.controller.view.HomeTopNewsView;
import com.metaera.app.controller.view.RefreshLoadMoreView;
import com.metaera.app.controller.view.core.IPaginationView;
import com.metaera.app.data.model.ArticleListResponse;
import com.metaera.app.data.network.ApiParam;
import com.metaera.app.ui.adapters.HomeNewsAdapter;
import com.metaera.app.ui.core.BaseRequestFragment;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;

import androidx.recyclerview.widget.RecyclerView;


/**
 * 首页的头条新闻
 */
public class HomeTopNewsFragment extends BaseRequestFragment {

    @Override
    protected int getLayoutId() {

        return R.layout.include_smart_refresh_recycler_view;
    }

    @Override
    protected void initialize(Bundle savedInstanceState) {
        // find
        RecyclerView recyclerView = findViewById(R.id.recycler_view);
        SmartRefreshLayout refreshLayout = findViewById(R.id.smart_refresh_layout);

        // init view
//        DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(recyclerView.getContext(), DividerItemDecoration.VERTICAL);
//        Drawable drawable = ContextCompat.getDrawable(recyclerView.getContext(), R.drawable.shape_item_divider);
//        dividerItemDecoration.setDrawable(drawable);
//        recyclerView.addItemDecoration(dividerItemDecoration);
        recyclerView.setBackgroundColor(Color.WHITE);

        HomeTopNewsView homeTopNewsView = new HomeTopNewsView(recyclerView, this);
        HomeNewsAdapter adapter = new HomeNewsAdapter();
        adapter.addHeaderView(homeTopNewsView.getView());
        recyclerView.setAdapter(adapter);

        // network
        HomeTopNewsPresenter topNewsPresenter = new HomeTopNewsPresenter();
        topNewsPresenter.setModelAndView(homeTopNewsView);
        topNewsPresenter.execute();

        // list
        IPaginationPresenter<IPaginationView<ArticleListResponse>, ArticleListResponse>
                listPresenter = new HomeNewsListPresenter(ApiParam.TYPE_HOME_TOP_NEWS);
        BasicPaginationView<ArticleListResponse> view = new BasicPaginationView<>(recyclerView, refreshLayout, this);
        view.setNotMoreFooterLayoutResId(R.layout.view_list_not_more); // 没有更多
        listPresenter.setModelAndView(view);
        listPresenter.requestRefresh();

        // listener
        refreshLayout.setOnRefreshLoadMoreListener(new RefreshLoadMoreView<>(listPresenter));
    }

}
